package com.hns.can.service.service;


import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.hns.can.common.entity.BaseEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hns.can.common.aop.orgTree.LeftOrgAnnotation;
import com.hns.can.common.constants.GlobalConst;
import com.hns.can.common.formula.FillCodeDscValue;
import com.hns.can.common.formula.FillParamsVo;
import com.hns.can.service.entity.BaseStnBevParamInfoEntity;
import com.hns.can.service.entity.BaseStnDuraParamInfoEntity;
import com.hns.can.service.entity.BsDayStnBevDetailEntity;
import com.hns.can.service.entity.BsDayStnDuraDetailEntity;
import com.hns.can.service.vo.LineParameterVo;
import com.hns.can.service.vo.StationAlarmDelayVo;
import com.hns.frame.mybatis.Pagination;
import com.hns.frame.service.CommonService;
import com.hns.tool.pub.DataConvertUtil;
import com.hns.tool.pub.PubUtil;

/**
 * 线路参数设置
 * @author:lan
 * @email:763836737@qq.com
 * @version Revision 2.0.0
 */
@Service
public class OperationReportService extends CommonService {
    
    /**
     * 填充代码描述
     */
    @Autowired
    private FillCodeDscValue codeDscValue;
    
    /**
     * 
     * @return
     * @author:Lan
     * @email:763836737@qq.com
     * @创建日期:2015-6-18
     * @功能说明：获取版本
     */
    @SuppressWarnings("unchecked")
    @Transactional
    public List<BaseStnBevParamInfoEntity> getVrsnName(LineParameterVo vo) throws Exception {
        List<BaseStnBevParamInfoEntity> list = PubUtil.convertListMapToObject(sqlMapDao.selectList("stationSet.getVrsnName", vo), BaseStnBevParamInfoEntity.class);
        return list;
    }

    /**
     * 
     * @throws Exception
     * @author:Lan
     * @email:763836737@qq.com
     * @创建日期:2015-6-19
     * @功能说明：查询线路信息
     */
    @SuppressWarnings("unchecked")
    @Transactional
    public List<LineParameterVo> searchStationInfo(LineParameterVo vo) throws Exception {
        List<LineParameterVo> list = PubUtil.convertListMapToObject( 
        this.sqlMapDao.selectList("stationSet.searchWorkStationInfo", vo), BaseStnBevParamInfoEntity.class);
        Map<String,String> map = new HashMap<String,String>();
        map.put("beginStnName", GlobalConst.DEFAULT_CACHE_VALUE);  
        map.put("endStnName", GlobalConst.DEFAULT_CACHE_VALUE);
        FillParamsVo vo1 = new FillParamsVo("stnId", map);
        codeDscValue.fillInfoValue(list, new String[] {GlobalConst.CACHE_KEY_BASE_STN_INFO}, vo1);
        return list;
    }
    
    /**
     * 
     * @throws Exception
     * @author:Lan
     * @email:763836737@qq.com
     * @创建日期:2015-6-19
     * @功能说明：根据版本查询线路信息
     */
    @SuppressWarnings("unchecked")
    @Transactional
    public List<LineParameterVo> searchlineParameterInfo(LineParameterVo vo) throws Exception {
        List<LineParameterVo> list = PubUtil.convertListMapToObject(
        this.sqlMapDao.selectList("stationSet.searchlineParameterInfo", vo), BaseStnBevParamInfoEntity.class);
        Map<String,String> map = new HashMap<String,String>();
        map.put("beginStnName", GlobalConst.DEFAULT_CACHE_VALUE);  
        map.put("endStnName", GlobalConst.DEFAULT_CACHE_VALUE);
        FillParamsVo vo1 = new FillParamsVo("stnId", map);
        codeDscValue.fillInfoValue(list, new String[] {GlobalConst.CACHE_KEY_BASE_STN_INFO}, vo1);
        return list;
    }
    
    /**
     * 
     * @return
     * @author:Lan
     * @email:763836737@qq.com
     * @创建日期:2015-6-24
     * @功能说明：是否存在相同版本
     */
    @Transactional
    public int checkVersion(LineParameterVo vo) throws Exception {
        int count = (Integer) sqlMapDao.selectOne("stationSet.checkVersion", vo);
        return count;
    }
    
    /**
     * 
     * @return
     * @author:Lan
     * @email:763836737@qq.com
     * @创建日期:2015-6-24
     * @功能说明：相同路线日期内 是否存在交集时间
     */
    @Transactional
    public int checkByData(LineParameterVo vo) throws Exception {
        int count = (Integer) sqlMapDao.selectOne("stationSet.checkByData", vo);
        return count;
    }
    
    /**
     * 
     * @author:Lan
     * @email:763836737@qq.com
     * @创建日期:2015-6-24
     * @功能说明：保存
     */
    @Transactional
    public void saveStationBehavior(LineParameterVo vo) throws Exception {
        String[] lineType=vo.getLineType().split(",");
        String[] stnCd=vo.getStnCd().split(",");
        String[] stnSn=vo.getStnSn().toString().split(",");
        String[] stayStnDura=vo.getStayStnDura().toString().split(",");
        String[] aheadStnDura=vo.getAheadStnDura().toString().split(",");
        String[] vrsnCd=vo.getVrsnCd().split(",");
        for(int i=0;i<stnSn.length;i++){
            BaseStnBevParamInfoEntity saveEntity = new BaseStnBevParamInfoEntity();
            saveEntity.setCorpId(vo.getCorpId());
            saveEntity.setVrsnName(vo.getVrsnName());
            saveEntity.setBeginTime(vo.getBeginTime());
            saveEntity.setEndTime(vo.getEndTime().substring(0,8));
            saveEntity.setDateType(vo.getDateType());
            saveEntity.setStrtusDate(vo.getStrtusDate());
            saveEntity.setLineId(vo.getLineId());
            saveEntity.setLineType(lineType[i]);
            saveEntity.setStnCd(stnCd[i]);
            saveEntity.setStnSn(new BigDecimal(stnSn[i]));
            saveEntity.setStayStnDura(new BigDecimal(stayStnDura[i]));
            saveEntity.setAheadStnDura(new BigDecimal(aheadStnDura[i]));
            saveEntity.setCrtTime(new Date());   
            saveEntity.setCrtUser(vo.getCrtUser());  
            saveEntity.setUpdateTime(new Date());
            saveEntity.setUpdateUser(vo.getCrtUser());
            if(vrsnCd.length==1){
                //新增
                saveEntity.setVrsnCd(UUID.randomUUID().toString().replace("-", ""));
                commonEntityDao.save(saveEntity);
            }else{
                //修改
                saveEntity.setVrsnCd(vrsnCd[i]);
                commonEntityDao.update(saveEntity);
            }
        }
    }
    
    /**
     * 
     * @param vo
     * @author:Lan
     * @email:763836737@qq.com
     * @创建日期:2015-7-1
     * @功能说明：删除
     */
    @SuppressWarnings({"unchecked", "rawtypes" })
    @Transactional
    public void delStationBehavior(LineParameterVo vo) throws Exception {
        Map map = DataConvertUtil.ConvertObjToMap(vo);
        commonEntityDao.deleteByParamsIgnoreNullValue(BaseStnBevParamInfoEntity.class, map);
    }
    
    /**
     * 
     * @param entity
     * @return
     * @author:Lan
     * @email:763836737@qq.com
     * @创建日期:2015-6-18
     * @功能说明：获取站间时长版本——站间时长
     */
    @SuppressWarnings("unchecked")
    @Transactional
    public List<BaseStnBevParamInfoEntity> getStnDuraVrsnName(BaseStnDuraParamInfoEntity entity) throws Exception {
        List<BaseStnBevParamInfoEntity> list = PubUtil.convertListMapToObject(sqlMapDao.selectList("stationSet.getStnDuraVrsnName", entity), BaseStnDuraParamInfoEntity.class);
        return list;
    }
    
    /**
     * 
     * @throws Exception
     * @author:Lan
     * @email:763836737@qq.com
     * @创建日期:2015-6-19
     * @功能说明：根据版本查询线路信息——站间时长
     */
    @SuppressWarnings("unchecked")
    @Transactional
    public List<LineParameterVo>  searchStnDuraInfo(LineParameterVo vo) throws Exception {
        List<LineParameterVo> list = PubUtil.convertListMapToObject(
        this.sqlMapDao.selectList("stationSet.searchStnDuraInfo", vo), BaseStnDuraParamInfoEntity.class);
        return list;
    }
    
    /**
     * 
     * @return
     * @author:Lan
     * @email:763836737@qq.com
     * @创建日期:2015-6-24
     * @功能说明：是否存在相同版本——站间时长
     */
    @Transactional
    public int checkStnDura(LineParameterVo vo) throws Exception {
        int count = (Integer) sqlMapDao.selectOne("stationSet.checkStnDura", vo);
        return count;
    }
    
    /**
     * 
     * @return
     * @author:Lan
     * @email:763836737@qq.com
     * @创建日期:2015-6-24
     * @功能说明：相同路线日期内 是否存在交集时间——站间时长
     */
    @Transactional
    public int checkStnDuraByData(LineParameterVo vo) throws Exception {
        int count = (Integer) sqlMapDao.selectOne("stationSet.checkStnDuraByData", vo);
        return count;
    }
    
    /**
     * 
     * @author:Lan
     * @email:763836737@qq.com
     * @创建日期:2015-6-24
     * @功能说明：保存——站间时长
     */
    @Transactional
    public void saveStnDura(LineParameterVo vo) throws Exception {
        String[] beginStnCd=vo.getBeginStnCd().split(",");
        String[] beginStnSn=vo.getBeginStnSn().toString().split(",");
        String[] minStnDura=vo.getMinStnDura().toString().split(",");
        String[] maxStnDura=vo.getMaxStnDura().toString().split(",");
        String[] vrsnCd=vo.getVrsnCd().split(",");
        String[] strtusDateArr=vo.getStrtusDateArr().split(",");
        for(int i=0;i<beginStnSn.length;i++){
            BaseStnDuraParamInfoEntity entity = new BaseStnDuraParamInfoEntity();
            entity.setCorpId(vo.getCorpId());
            entity.setMinStnDura(new BigDecimal(minStnDura[i]));
            entity.setMaxStnDura(new BigDecimal(maxStnDura[i]));
            entity.setCrtTime(new Date());   
            entity.setCrtUser(vo.getCrtUser());  
            entity.setUpdateTime(new Date());
            entity.setUpdateUser(vo.getCrtUser());
            String endTime = vo.getEndTime().substring(0,8);
            if(vrsnCd.length<1 || vrsnCd[i] == ""){
                //新增
                entity.setVrsnName(vo.getVrsnName());
                entity.setBeginTime(vo.getBeginTime());
                entity.setEndTime(endTime);
                entity.setDateType(vo.getDateType());
                entity.setStrtusDate(vo.getStrtusDate());
                entity.setLineId(vo.getLineId());
                entity.setLineType(vo.getLineType());
                entity.setBeginStnSn(new BigDecimal(vo.getBeginStnSn()));
                entity.setEndStnCd(vo.getEndStnCd());
                entity.setEndStnSn(new BigDecimal(vo.getEndStnSn()));
                entity.setVrsnCd(UUID.randomUUID().toString().replace("-", ""));
                entity.setBeginStnCd(beginStnCd[i]);
                commonEntityDao.save(entity);
            }else{
                //修改
                    entity.setVrsnName(vo.getVrsnName());
                    entity.setBeginTime(vo.getBeginTime());
                    entity.setEndTime(endTime);
                    entity.setDateType(vo.getDateType());
                    entity.setLineId(vo.getLineId());
                    entity.setLineType(vo.getLineType());
                    entity.setStrtusDate(vo.getStrtusDate());
                    entity.setBeginStnSn(new BigDecimal(vo.getBeginStnSn()));
                    entity.setEndStnCd(vo.getEndStnCd());
                    entity.setEndStnSn(new BigDecimal(vo.getEndStnSn()));
                    entity.setVrsnCd(vrsnCd[i]);
                    BaseStnDuraParamInfoEntity cdEntity = commonEntityDao.find(entity.getVrsnCd(), BaseStnDuraParamInfoEntity.class);
                    entity.setBeginStnCd(cdEntity.getBeginStnCd());
                    entity.setEndStnCd(cdEntity.getEndStnCd());
                    commonEntityDao.update(entity);
            }
        }
    }
    
    /**
     * 
     * @param
     * @author:Lan
     * @email:763836737@qq.com
     * @创建日期:2015-7-1
     * @功能说明：删除——站间时长
     */
    @SuppressWarnings({"unchecked", "rawtypes" })
    @Transactional
    public void delStnDura(String[] ids) throws Exception{
//        Map map = DataConvertUtil.ConvertObjToMap(vo);
        commonEntityDao.delete(BaseStnDuraParamInfoEntity.class, ids);
    }
    
    /**
     * 
     * @return
     * @author:Lan
     * @email:763836737@qq.com
     * @创建日期:2015-6-18
     * @功能说明：获取版本
     */
//    @SuppressWarnings("unchecked")
//    @Transactional
//    public List<LineParameterVo>  getStnDuraByLineType(LineParameterVo vo) throws Exception {
//        List<LineParameterVo> list = PubUtil.convertListMapToObject(
//        this.sqlMapDao.selectList("stationSet.getStnDuraByLineType", vo), BaseStnDuraParamInfoEntity.class);
//        return list;
//    }
    
    /**
     * 
     * @return
     * @author:Lan
     * @email:763836737@qq.com
     * @创建日期:2015-7-9
     * @功能说明：查询滞站越站监控
     */
    @Transactional
    @LeftOrgAnnotation()
    public Pagination searchStationBehaviorMonitor(LineParameterVo entity)throws Exception {
        Pagination page = this.myBatisPage("lineParameterMonitor.searchStationBehaviorMonitorCount", "lineParameterMonitor.searchStationBehaviorMonitor", entity, StationAlarmDelayVo.class);
        List<Object> list = page.getData();
        Map<String,String> map = new HashMap<String,String>();
        map.put("licPltNo", GlobalConst.DEFAULT_CACHE_VALUE);
        map.put("engiNo", "engiNo");
        FillParamsVo vo1 = new FillParamsVo("carId", map);
        Map<String,String> map2 = new HashMap<String,String>();
        map2.put("organName", GlobalConst.DEFAULT_CACHE_VALUE);
        FillParamsVo vo2 = new FillParamsVo("organId", map2);
        Map<String,String> map3 = new HashMap<String,String>();
        map3.put("drvName", GlobalConst.DEFAULT_CACHE_VALUE);
        FillParamsVo vo3 = new FillParamsVo("drvId", map3);
        Map<String,String> map4 = new HashMap<String,String>();
        map4.put("lineName", GlobalConst.DEFAULT_CACHE_VALUE);
        FillParamsVo vo4 = new FillParamsVo("lineId", map4);
        codeDscValue.fillInfoValue(list, new String[] {GlobalConst.CACHE_KEY_BASE_CAR_INFO, GlobalConst.CACHE_KEY_BASE_ORGAN, GlobalConst.CACHE_KEY_BASE_DRV_INFO, GlobalConst.CACHE_KEY_BASE_LINE_INFO},vo1, vo2,vo3,vo4);
        if(!PubUtil.isEmpty(list)){
        	for(int i=0;i<list.size();i++){
            	StationAlarmDelayVo vo = (StationAlarmDelayVo) list.get(i);
            	if("1".equals(vo.getWthrCar())){
            		vo.setWthrCar("是");
            	}else{
            		vo.setWthrCar("否");
            	}
            	if("1".equals(vo.getWthrSpdOfMotVhi())){
            		vo.setWthrSpdOfMotVhi("是");
            	}else{
            		vo.setWthrSpdOfMotVhi("否");
            	}
            }
        }
        
        page.setData(list);
        return page;
    }
    
    /**
     * 
     * @return
     * @author:Lan
     * @email:763836737@qq.com
     * @创建日期:2014-7-11
     * @功能说明： 导出滞站越站监控 
     */
    @SuppressWarnings("unchecked")
    @Transactional
    @LeftOrgAnnotation()
    public List<BaseEntity> getStationBehaviorMonitorList(LineParameterVo entity)  throws Exception{
        List<BaseEntity> list = PubUtil.convertListMapToObject(sqlMapDao.selectList("lineParameterMonitor.searchStationBehaviorMonitor", entity), StationAlarmDelayVo.class);
        Map<String,String> map = new HashMap<String,String>();
        map.put("licPltNo", GlobalConst.DEFAULT_CACHE_VALUE);
        map.put("engiNo", "engiNo");
        FillParamsVo vo1 = new FillParamsVo("carId", map);
        Map<String,String> map2 = new HashMap<String,String>();
        map2.put("organName", GlobalConst.DEFAULT_CACHE_VALUE);
        FillParamsVo vo2 = new FillParamsVo("organId", map2);
        Map<String,String> map3 = new HashMap<String,String>();
        map3.put("drvName", GlobalConst.DEFAULT_CACHE_VALUE);
        FillParamsVo vo3 = new FillParamsVo("drvId", map3);
        Map<String,String> map4 = new HashMap<String,String>();
        map4.put("lineName", GlobalConst.DEFAULT_CACHE_VALUE);
        FillParamsVo vo4 = new FillParamsVo("lineId", map4);
        codeDscValue.fillInfoValue(list, new String[] {GlobalConst.CACHE_KEY_BASE_CAR_INFO, GlobalConst.CACHE_KEY_BASE_ORGAN, GlobalConst.CACHE_KEY_BASE_DRV_INFO, GlobalConst.CACHE_KEY_BASE_LINE_INFO},vo1, vo2,vo3,vo4);
        Map<String,String> mapTmp = new HashMap<String,String>();
        mapTmp.put("lineType", "LIT00000");
        mapTmp.put("cgy", "STNB0000");
        codeDscValue.fillCodeDscValue(list, mapTmp);
        return list;
    }
    
    /**
     * 
     * @return
     * @author:Lan
     * @email:763836737@qq.com
     * @创建日期:2015-7-9
     * @功能说明：查询站间时长监控
     */
    @Transactional
    @LeftOrgAnnotation()
    public Pagination searchStnDuraMonitor(LineParameterVo entity)throws Exception {
        Pagination page = this.myBatisPage("lineParameterMonitor.searchStnDuraMonitorCount", "lineParameterMonitor.searchStnDuraMonitor", entity, BsDayStnDuraDetailEntity.class);
        List<Object> list = page.getData();
        Map<String,String> map = new HashMap<String,String>();
        map.put("licPltNo", GlobalConst.DEFAULT_CACHE_VALUE);
        map.put("engiNo", "engiNo");
        FillParamsVo vo1 = new FillParamsVo("carId", map);
        Map<String,String> map2 = new HashMap<String,String>();
        map2.put("organName", GlobalConst.DEFAULT_CACHE_VALUE);
        FillParamsVo vo2 = new FillParamsVo("organId", map2);
        Map<String,String> map3 = new HashMap<String,String>();
        map3.put("drvName", GlobalConst.DEFAULT_CACHE_VALUE);
        FillParamsVo vo3 = new FillParamsVo("drvId", map3);
        Map<String,String> map4 = new HashMap<String,String>();
        map4.put("lineName", GlobalConst.DEFAULT_CACHE_VALUE);
        FillParamsVo vo4 = new FillParamsVo("lineId", map4);
        codeDscValue.fillInfoValue(list, new String[] {GlobalConst.CACHE_KEY_BASE_CAR_INFO, GlobalConst.CACHE_KEY_BASE_ORGAN, GlobalConst.CACHE_KEY_BASE_DRV_INFO, GlobalConst.CACHE_KEY_BASE_LINE_INFO},vo1, vo2,vo3,vo4);
        return page;
    }
    
    /**
     * 
     * @return
     * @author:Lan
     * @email:763836737@qq.com
     * @创建日期:2014-7-11
     * @功能说明： 导出站间时长监控 
     */
    @SuppressWarnings("unchecked")
    @Transactional
    @LeftOrgAnnotation()
    public List<BsDayStnDuraDetailEntity> getStnDuraMonitorList(BsDayStnDuraDetailEntity entity)  throws Exception{
        List<BsDayStnDuraDetailEntity> list = PubUtil.convertListMapToObject(sqlMapDao.selectList("lineParameterMonitor.searchStnDuraMonitor", entity), BsDayStnDuraDetailEntity.class);
        Map<String,String> map = new HashMap<String,String>();
        map.put("licPltNo", GlobalConst.DEFAULT_CACHE_VALUE);
        map.put("engiNo", "engiNo");
        FillParamsVo vo1 = new FillParamsVo("carId", map);
        Map<String,String> map2 = new HashMap<String,String>();
        map2.put("organName", GlobalConst.DEFAULT_CACHE_VALUE);
        FillParamsVo vo2 = new FillParamsVo("organId", map2);
        Map<String,String> map3 = new HashMap<String,String>();
        map3.put("drvName", GlobalConst.DEFAULT_CACHE_VALUE);
        FillParamsVo vo3 = new FillParamsVo("drvId", map3);
        Map<String,String> map4 = new HashMap<String,String>();
        map4.put("lineName", GlobalConst.DEFAULT_CACHE_VALUE);
        FillParamsVo vo4 = new FillParamsVo("lineId", map4);
        codeDscValue.fillInfoValue(list, new String[] {GlobalConst.CACHE_KEY_BASE_CAR_INFO, GlobalConst.CACHE_KEY_BASE_ORGAN, GlobalConst.CACHE_KEY_BASE_DRV_INFO, GlobalConst.CACHE_KEY_BASE_LINE_INFO},vo1, vo2,vo3,vo4);
        return list;
    }
}